﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OptimizedDataTable;

namespace FilterData
{
    public partial class Form1 : Form
    {
        IndexedDataTable idt;
        public Form1()
        {
            InitializeComponent();

            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("SampleProc", new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString));
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            idt = new IndexedDataTable(dt);
            idt.SetColumnsToIndex(new List<string>() { "ProductID", "TransactionDate", "Color" });
            List<string> lstOperations1 = new List<string>() { "All", "Equal", "LessThan", "GreaterThan", "NotEqual", "LessThanOrEqual", "GreaterThanOrEqual", "In" };
            List<string> lstOperations2 = new List<string>() { "All", "Equal", "LessThan", "GreaterThan", "NotEqual", "LessThanOrEqual", "GreaterThanOrEqual", "In" };
            List<string> lstOperations3 = new List<string>() { "All", "Equal", "LessThan", "GreaterThan", "NotEqual", "LessThanOrEqual", "GreaterThanOrEqual", "In" };
            comboBox1.DataSource = lstOperations1;
            comboBox2.DataSource = lstOperations2;
            comboBox3.DataSource = lstOperations3;
            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<DataRow> lst = new List<DataRow>();
            
            idt.FindAll(new List<RowFetcher>() { 
            new RowFetcher(){ColumnName="ProductID",Operation=(Operator)Enum.Parse(typeof(Operator),(comboBox1.SelectedItem.ToString())),Val=int.Parse(textBox1.Text)},
            new RowFetcher(){ColumnName="TransactionDate",Operation=(Operator)Enum.Parse(typeof(Operator),(comboBox2.SelectedItem.ToString())),Val=Convert.ToDateTime(textBox2.Text)},
            new RowFetcher(){ColumnName="Color",Operation=(Operator)Enum.Parse(typeof(Operator),(comboBox3.SelectedItem.ToString())),Val=textBox3.Text}
            }, ref lst);
            dataGridView1.DataSource = lst;
        }
    }
}
